package com.yicong.ants.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.yicong.ants.R;
import com.yicong.ants.manager.js.ShowPicRelation;

/**
 * 查看门票 popupWindow
 */
public class ToCheckTicketsPop extends BasePopupWindow {
    private ImageView ivBackUp, ivAdvance;
    private TextView tvTitle;
    private X5WebViewBackup webView;
    private boolean isImg;

    public ToCheckTicketsPop(Context context) {
        super(context);
    }

    public boolean canGoBackWebView() {
        if (null == webView) {
            return false;
        }
        if (webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return false;
    }

    public void setHtmlContent(String htmlContent, boolean img) {
        isImg = img;
        String content;
        if (isImg) {
            content = "<html>" +
                "<body bgcolor='white'>" +
                "<div align=\"center\"><img src=\"" + htmlContent +
                "\"/></div>" +
                "</body>" +
                "</html>";
        } else {
            content = "<html>" +
                "<body bgcolor='white'>" +
                "<div align=\"center\" style=\"width:100%;height:100%;padding-top:10%;word-wrap:break-word\">" + htmlContent +
                "</div>" +
                "</body>" +
                "</html>";
        }
        webView.loadDataWithBaseURL(null, content, "text/html", "utf-8",
            null);
    }

    @Override
    public void initSetting() {
        setWidth(FrameLayout.LayoutParams.MATCH_PARENT);
        setHeight(FrameLayout.LayoutParams.MATCH_PARENT);
        isBackGround = false;
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initView() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.popup_window_check_tickets, null);
        // 返回
        ivBackUp = mView.findViewById(R.id.iv_back_up);
        // 前进
        ivAdvance = mView.findViewById(R.id.iv_advance);
        tvTitle = mView.findViewById(R.id.tv_title);
        webView = mView.findViewById(R.id.tickets_webview);
        ivBackUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (webView.canGoBack()) {
                    webView.goBack();
                } else {
                    dismiss();
                }
            }
        });
        ivAdvance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (webView.canGoForward()) {
                    webView.goForward();
                }
            }
        });
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (webView.canGoBack()) {
                    tvTitle.setText(mContext.getResources().getString(R.string.click_to_enlarge));
                } else {
                    tvTitle.setText(mContext.getResources().getString(R.string.ticket_information));
                }
                if (webView.canGoForward()) {
                    ivAdvance.setImageResource(R.drawable.icon_advance_true);
                } else {
                    ivAdvance.setImageResource(R.drawable.icon_advance);
                }
            }
        });
        webView.setHtmlTextSize(100, 18);
        webView.addJavascriptInterface(new ShowPicRelation(mContext), "rzj");
        webView.loadUrl(ShowPicRelation.getJsForImg());
    }
}
